/*
 * Copyright (C) 2005-2011 MaNGOS <http://www.getmangos.com/>
 *
 * Copyright (C) 2008-2011 Trinity <http://www.trinitycore.org/>
 *
 * Copyright (C) 2010-2011 ArkCORE <http://www.arkania.net/>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#ifndef ARKCORE_GRIDNOTIFIERSIMPL_H
#define ARKCORE_GRIDNOTIFIERSIMPL_H

#include "GridNotifiers.h"
#include "WorldPacket.h"
#include "Corpse.h"
#include "Player.h"
#include "UpdateData.h"
#include "CreatureAI.h"
#include "SpellAuras.h"

template<class T>
inline void Trinity::VisibleNotifier::Visit(GridRefManager<T> &m) {
	for (typename GridRefManager<T>::iterator iter = m.begin(); iter != m.end();
			++iter) {
		vis_guids.erase(iter->getSource()->GetGUID());
		i_player.UpdateVisibilityOf(iter->getSource(), i_data, i_visibleNow);
	}
}

inline void Trinity::ObjectUpdater::Visit(CreatureMapType &m) {
	for (CreatureMapType::iterator iter = m.begin(); iter != m.end(); ++iter)
		if (iter->getSource()->IsInWorld())
			iter->getSource()->Update(i_timeDiff);
}

// SEARCHERS & LIST SEARCHERS & WORKERS

// WorldObject searchers & workers

template<class Check>
void Trinity::WorldObjectSearcher<Check>::Visit(GameObjectMapType &m) {
	// already found
	if (i_object)
		return;

	for (GameObjectMapType::iterator itr = m.begin(); itr != m.end(); ++itr) {
		if (!itr->getSource()->InSamePhase(i_phaseMask))
			continue;

		if (i_check(itr->getSource())) {
			i_object = itr->getSource();
			return;
		}
	}
}

template<class Check>
void Trinity::WorldObjectSearcher<Check>::Visit(PlayerMapType &m) {
	// already found
	if (i_object)
		return;

	for (PlayerMapType::iterator itr = m.begin(); itr != m.end(); ++itr) {
		if (!itr->getSource()->InSamePhase(i_phaseMask))
			continue;

		if (i_check(itr->getSource())) {
			i_object = itr->getSource();
			return;
		}
	}
}

template<class Check>
void Trinity::WorldObjectSearcher<Check>::Visit(CreatureMapType &m) {
	// already found
	if (i_object)
		return;

	for (CreatureMapType::iterator itr = m.begin(); itr != m.end(); ++itr) {
		if (!itr->getSource()->InSamePhase(i_phaseMask))
			continue;

		if (i_check(itr->getSource())) {
			i_object = itr->getSource();
			return;
		}
	}
}

template<class Check>
void Trinity::WorldObjectSearcher<Check>::Visit(CorpseMapType &m) {
	// already found
	if (i_object)
		return;

	for (CorpseMapType::iterator itr = m.begin(); itr != m.end(); ++itr) {
		if (!itr->getSource()->InSamePhase(i_phaseMask))
			continue;

		if (i_check(itr->getSource())) {
			i_object = itr->getSource();
			return;
		}
	}
}

template<class Check>
void Trinity::WorldObjectSearcher<Check>::Visit(DynamicObjectMapType &m) {
	// already found
	if (i_object)
		return;

	for (DynamicObjectMapType::iterator itr = m.begin(); itr != m.end();
			++itr) {
		if (!itr->getSource()->InSamePhase(i_phaseMask))
			continue;

		if (i_check(itr->getSource())) {
			i_object = itr->getSource();
			return;
		}
	}
}

template<class Check>
void Trinity::WorldObjectListSearcher<Check>::Visit(PlayerMapType &m) {
	for (PlayerMapType::iterator itr = m.begin(); itr != m.end(); ++itr)
		if (itr->getSource()->InSamePhase(i_phaseMask))
			if (i_check(itr->getSource()))
				i_objects.push_back(itr->getSource());
}

template<class Check>
void Trinity::WorldObjectListSearcher<Check>::Visit(CreatureMapType &m) {
	for (CreatureMapType::iterator itr = m.begin(); itr != m.end(); ++itr)
		if (itr->getSource()->InSamePhase(i_phaseMask))
			if (i_check(itr->getSource()))
				i_objects.push_back(itr->getSource());
}

template<class Check>
void Trinity::WorldObjectListSearcher<Check>::Visit(CorpseMapType &m) {
	for (CorpseMapType::iterator itr = m.begin(); itr != m.end(); ++itr)
		if (itr->getSource()->InSamePhase(i_phaseMask))
			if (i_check(itr->getSource()))
				i_objects.push_back(itr->getSource());
}

template<class Check>
void Trinity::WorldObjectListSearcher<Check>::Visit(GameObjectMapType &m) {
	for (GameObjectMapType::iterator itr = m.begin(); itr != m.end(); ++itr)
		if (itr->getSource()->InSamePhase(i_phaseMask))
			if (i_check(itr->getSource()))
				i_objects.push_back(itr->getSource());
}

template<class Check>
void Trinity::WorldObjectListSearcher<Check>::Visit(DynamicObjectMapType &m) {
	for (DynamicObjectMapType::iterator itr = m.begin(); itr != m.end(); ++itr)
		if (itr->getSource()->InSamePhase(i_phaseMask))
			if (i_check(itr->getSource()))
				i_objects.push_back(itr->getSource());
}

// Gameobject searchers

template<class Check>
void Trinity::GameObjectSearcher<Check>::Visit(GameObjectMapType &m) {
	// already found
	if (i_object)
		return;

	for (GameObjectMapType::iterator itr = m.begin(); itr != m.end(); ++itr) {
		if (!itr->getSource()->InSamePhase(i_phaseMask))
			continue;

		if (i_check(itr->getSource())) {
			i_object = itr->getSource();
			return;
		}
	}
}

template<class Check>
void Trinity::GameObjectLastSearcher<Check>::Visit(GameObjectMapType &m) {
	for (GameObjectMapType::iterator itr = m.begin(); itr != m.end(); ++itr) {
		if (!itr->getSource()->InSamePhase(i_phaseMask))
			continue;

		if (i_check(itr->getSource()))
			i_object = itr->getSource();
	}
}

template<class Check>
void Trinity::GameObjectListSearcher<Check>::Visit(GameObjectMapType &m) {
	for (GameObjectMapType::iterator itr = m.begin(); itr != m.end(); ++itr)
		if (itr->getSource()->InSamePhase(i_phaseMask))
			if (i_check(itr->getSource()))
				i_objects.push_back(itr->getSource());
}

// Unit searchers

template<class Check>
void Trinity::UnitSearcher<Check>::Visit(CreatureMapType &m) {
	// already found
	if (i_object)
		return;

	for (CreatureMapType::iterator itr = m.begin(); itr != m.end(); ++itr) {
		if (!itr->getSource()->InSamePhase(i_phaseMask))
			continue;

		if (i_check(itr->getSource())) {
			i_object = itr->getSource();
			return;
		}
	}
}

template<class Check>
void Trinity::UnitSearcher<Check>::Visit(PlayerMapType &m) {
	// already found
	if (i_object)
		return;

	for (PlayerMapType::iterator itr = m.begin(); itr != m.end(); ++itr) {
		if (!itr->getSource()->InSamePhase(i_phaseMask))
			continue;

		if (i_check(itr->getSource())) {
			i_object = itr->getSource();
			return;
		}
	}
}

template<class Check>
void Trinity::UnitLastSearcher<Check>::Visit(CreatureMapType &m) {
	for (CreatureMapType::iterator itr = m.begin(); itr != m.end(); ++itr) {
		if (!itr->getSource()->InSamePhase(i_phaseMask))
			continue;

		if (i_check(itr->getSource()))
			i_object = itr->getSource();
	}
}

template<class Check>
void Trinity::UnitLastSearcher<Check>::Visit(PlayerMapType &m) {
	for (PlayerMapType::iterator itr = m.begin(); itr != m.end(); ++itr) {
		if (!itr->getSource()->InSamePhase(i_phaseMask))
			continue;

		if (i_check(itr->getSource()))
			i_object = itr->getSource();
	}
}

template<class Check>
void Trinity::UnitListSearcher<Check>::Visit(PlayerMapType &m) {
	for (PlayerMapType::iterator itr = m.begin(); itr != m.end(); ++itr)
		if (itr->getSource()->InSamePhase(i_phaseMask))
			if (i_check(itr->getSource()))
				i_objects.push_back(itr->getSource());
}

template<class Check>
void Trinity::UnitListSearcher<Check>::Visit(CreatureMapType &m) {
	for (CreatureMapType::iterator itr = m.begin(); itr != m.end(); ++itr)
		if (itr->getSource()->InSamePhase(i_phaseMask))
			if (i_check(itr->getSource()))
				i_objects.push_back(itr->getSource());
}

// Creature searchers

template<class Check>
void Trinity::CreatureSearcher<Check>::Visit(CreatureMapType &m) {
	// already found
	if (i_object)
		return;

	for (CreatureMapType::iterator itr = m.begin(); itr != m.end(); ++itr) {
		if (!itr->getSource()->InSamePhase(i_phaseMask))
			continue;

		if (i_check(itr->getSource())) {
			i_object = itr->getSource();
			return;
		}
	}
}

template<class Check>
void Trinity::CreatureLastSearcher<Check>::Visit(CreatureMapType &m) {
	for (CreatureMapType::iterator itr = m.begin(); itr != m.end(); ++itr) {
		if (!itr->getSource()->InSamePhase(i_phaseMask))
			continue;

		if (i_check(itr->getSource()))
			i_object = itr->getSource();
	}
}

template<class Check>
void Trinity::CreatureListSearcher<Check>::Visit(CreatureMapType &m) {
	for (CreatureMapType::iterator itr = m.begin(); itr != m.end(); ++itr)
		if (itr->getSource()->InSamePhase(i_phaseMask))
			if (i_check(itr->getSource()))
				i_objects.push_back(itr->getSource());
}

template<class Check>
void Trinity::PlayerListSearcher<Check>::Visit(PlayerMapType &m) {
	for (PlayerMapType::iterator itr = m.begin(); itr != m.end(); ++itr)
		if (itr->getSource()->InSamePhase(i_phaseMask))
			if (i_check(itr->getSource()))
				i_objects.push_back(itr->getSource());
}

template<class Check>
void Trinity::PlayerSearcher<Check>::Visit(PlayerMapType &m) {
	// already found
	if (i_object)
		return;

	for (PlayerMapType::iterator itr = m.begin(); itr != m.end(); ++itr) {
		if (!itr->getSource()->InSamePhase(i_phaseMask))
			continue;

		if (i_check(itr->getSource())) {
			i_object = itr->getSource();
			return;
		}
	}
}

template<class Check>
void Trinity::PlayerLastSearcher<Check>::Visit(PlayerMapType& m)
{
    for (PlayerMapType::iterator itr = m.begin(); itr != m.end(); ++itr)
    {
        if (!itr->getSource()->InSamePhase(i_phaseMask))
            continue;

        if (i_check(itr->getSource()))
            i_object = itr->getSource();
    }
}

template<class Builder>
void Trinity::LocalizedPacketDo<Builder>::operator()(Player* p) {
	LocaleConstant loc_idx = p->GetSession()->GetSessionDbLocaleIndex();
	uint32 cache_idx = loc_idx + 1;
	WorldPacket* data;

	// create if not cached yet
	if (i_data_cache.size() < cache_idx + 1 || !i_data_cache[cache_idx]) {
		if (i_data_cache.size() < cache_idx + 1)
			i_data_cache.resize(cache_idx + 1);

		data = new WorldPacket(SMSG_MESSAGECHAT, 200);

		i_builder(*data, loc_idx);

		i_data_cache[cache_idx] = data;
	} else
		data = i_data_cache[cache_idx];

	p->SendDirectMessage(data);
}

template<class Builder>
void Trinity::LocalizedPacketListDo<Builder>::operator()(Player* p) {
	LocaleConstant loc_idx = p->GetSession()->GetSessionDbLocaleIndex();
	uint32 cache_idx = loc_idx + 1;
	WorldPacketList* data_list;

	// create if not cached yet
	if (i_data_cache.size() < cache_idx + 1
			|| i_data_cache[cache_idx].empty()) {
		if (i_data_cache.size() < cache_idx + 1)
			i_data_cache.resize(cache_idx + 1);

		data_list = &i_data_cache[cache_idx];

		i_builder(*data_list, loc_idx);
	} else
		data_list = &i_data_cache[cache_idx];

	for (size_t i = 0; i < data_list->size(); ++i)
		p->SendDirectMessage((*data_list)[i]);
}

#endif                                                      // ARKCORE_GRIDNOTIFIERSIMPL_H
